package com.keehin;

import java.sql.*;

import net.sf.jasperreports.view.JasperViewer;
import net.sf.jasperreports.engine.util.JRLoader;
import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JREmptyDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRResultSetDataSource;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperPrintManager;
import net.sf.jasperreports.engine.JasperReport;
import java.util.*;

//import org.apache.derby.jdbc.EmbeddedDriver;

/**
 * Driver program to connect to a database and to view a jasper report (.jrxml)
 * 
 * @author Oguzhan Topsakal
 * @since 23 March 2006
 * 
 *        Required jar files to run this class: 1. jasperreports-1.2.0.jar 2.
 *        classes12.jar (for Oracle JDBC connection) 3.
 *        commons-beanutils-1.5.jar 4. commons-collections-2.1.jar 5.
 *        commons-digester-1.7.jar 6. commons-logging-1.0.2.jar
 * 
 */

@SuppressWarnings( { "unchecked", "unchecked" })
public class ReportDriver {

	/**
	 * Constructor for ReportDriver
	 */
	public ReportDriver() {
	}

	/**
	 * Takes 3 parameters: databaseName, userName, password and connects to the
	 * database.
	 * 
	 * @param databaseName
	 *            holds database name,
	 * @param userName
	 *            holds user name
	 * @param password
	 *            holds password to connect the database,
	 * @return Returns the JDBC connection to the database
	 */
	public static Connection connectDB(String databaseName, String userName,
			String password) {
		Connection jdbcConnection = null;
		try {
			// DriverManager.registerDriver(new EmbeddedDriver());
			jdbcConnection = DriverManager.getConnection(databaseName,
					userName, password);
		} catch (Exception ex) {
			String connectMsg = "Could not connect to the database: "
					+ ex.getMessage() + " " + ex.getLocalizedMessage();
			System.out.println(connectMsg);
		}
		return jdbcConnection;
	}

	private static JasperPrint renderReport(Connection jdbcConnection,
			Map parameters, String reportFile) {
		JasperPrint jasperPrint = null;
		try {
			JasperReport jasperReport = (JasperReport) JRLoader
					.loadObject(reportFile);
			jasperPrint = JasperFillManager.fillReport(jasperReport,
					parameters, jdbcConnection);
		} catch (JRException e) {
			e.printStackTrace();
		}
		return jasperPrint;
	}

	private static JasperPrint renderReport(Map parameters, String reportFile, int pages) {
		JasperPrint jasperPrint = null;
		try {
			JasperReport jasperReport = (JasperReport) JRLoader
					.loadObject(reportFile);
			jasperPrint = JasperFillManager
					.fillReport(jasperReport, parameters, new JREmptyDataSource(pages));
		} catch (JRException e) {
			e.printStackTrace();
		}
		return jasperPrint;
	}

	private static JasperPrint renderReport(ResultSet rs, Map parameters,
			String reportFile) {
		JasperPrint jasperPrint = null;
		try {
			JasperReport jasperReport = (JasperReport) JRLoader
					.loadObject(reportFile);
			jasperPrint = JasperFillManager.fillReport(jasperReport,
					parameters, new JRResultSetDataSource(rs));
		} catch (JRException e) {
			e.printStackTrace();
		}
		return jasperPrint;
	}

	public static void runReport(Map parameters, String reportFile, int pages) {
		JasperViewer.viewReport(renderReport(parameters, reportFile, pages), false);
	}

	public static void runReport(ResultSet rs, Map parameters, String reportFile) {
		// JasperDesign jasperDesign = JRXmlLoader.load(reportFile);
		// JasperReport jasperReport = JasperCompileManager
		// .compileReport(reportFile);
		JasperViewer
				.viewReport(renderReport(rs, parameters, reportFile), false);
	}

	public static void runReport(Connection jdbcConnection, Map parameters,
			String reportFile) {
		// JasperDesign jasperDesign = JRXmlLoader.load(reportFile);
		// JasperReport jasperReport = JasperCompileManager
		// .compileReport(reportFile);
		JasperViewer.viewReport(renderReport(jdbcConnection, parameters,
				reportFile), false);
	}

	public static void printReport(Connection jdbcConnection, Map parameters,
			String reportFile) {
		try {
			JasperPrintManager.printReport(renderReport(jdbcConnection,
					parameters, reportFile), false);
		} catch (JRException e) {
			e.printStackTrace();
		}
	}

}